package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.Shiwu;
import com.ruoyi.system.domain.Zhuanjia;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ZhuanjiaBiandongPici;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

/**
 * 专家变动批次Controller
 * 
 * @author ruoyi
 * @date 2022-12-08
 */
@Controller
@RequestMapping("/system/zhuanjiabiandongpici")
public class ZhuanjiaBiandongPiciController extends BaseController
{
    private String prefix = "system/zhuanjiabiandongpici";

    @Autowired
    private IZhuanjiaBiandongPiciService zhuanjiaBiandongPiciService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IBiandongPiciService biandongPiciService;

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;

    @RequiresPermissions("system:zhuanjiabiandongpici:view")
    @GetMapping()
    public String zhuanjiabiandongpici()
    {
        return prefix + "/zhuanjiabiandongpici";
    }

    @RequiresPermissions("system:zhuanjiabiandongpici:customview")
    @GetMapping("/customzhuanjiabiandongpici")
    public String customzhuanjiabiandongpici()
    {
        return prefix + "/customzhuanjiabiandongpici";
    }

    @RequiresPermissions("system:zhuanjiabiandongpici:customshenheview")
    @GetMapping("/customzhuanjiabiandongpici_shenhe")
    public String customzhuanjiabiandongpici_shenhe()
    {
        return prefix + "/customzhuanjiabiandongpici_shenhe";
    }

    /**
     * 查询专家变动批次列表
     */
    @RequiresPermissions("system:zhuanjiabiandongpici:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZhuanjiaBiandongPici zhuanjiaBiandongPici)
    {
        startPage();
        List<ZhuanjiaBiandongPici> list = zhuanjiaBiandongPiciService.selectZhuanjiaBiandongPiciList(zhuanjiaBiandongPici);
        return getDataTable(list);
    }

    /**
     * 导出专家变动批次列表
     */
    @RequiresPermissions("system:zhuanjiabiandongpici:export")
    @Log(title = "专家变动批次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZhuanjiaBiandongPici zhuanjiaBiandongPici)
    {
        List<ZhuanjiaBiandongPici> list = zhuanjiaBiandongPiciService.selectZhuanjiaBiandongPiciList(zhuanjiaBiandongPici);
        ExcelUtil<ZhuanjiaBiandongPici> util = new ExcelUtil<ZhuanjiaBiandongPici>(ZhuanjiaBiandongPici.class);
        return util.exportExcel(list, "专家变动批次数据");
    }

    /**
     * 新增专家变动批次
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存专家变动批次
     */
    @RequiresPermissions("system:zhuanjiabiandongpici:add")
    @Log(title = "专家变动批次", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZhuanjiaBiandongPici zhuanjiaBiandongPici)
    {
        return toAjax(zhuanjiaBiandongPiciService.insertZhuanjiaBiandongPici(zhuanjiaBiandongPici));
    }

    //@RequiresPermissions("system:zhuanjiabiandongpici:add")
    @Log(title = "专家变动批次", businessType = BusinessType.INSERT)
    @PostMapping("/customadd")
    @ResponseBody
    public JsonResult<?> customadd(ZhuanjiaBiandongPici zhuanjiaBiandongPici)
    {
        try{
                String mmDeptId = zhuanjiaBiandongPici.getDeptid();
                String mmOldDeptId = deptidOlddeptidService.selectDeptidOlddeptidByDeptid(mmDeptId).getOlddeptid();
                String mmDeptname = deptService.selectDeptById(mmOldDeptId).getJigoumingcheng();

                //String mmBiandongpici = zhuanjiaBiandongPici.getBeizhu1();
                String mmBiandongpiciname = zhuanjiaBiandongPici.getPicimingcheng();

                String mmId = UUID.randomUUID().toString();

                zhuanjiaBiandongPici.setId(mmId);
                zhuanjiaBiandongPici.setPicimingcheng(mmBiandongpiciname);
                zhuanjiaBiandongPici.setDanweimingcheng(mmDeptname);
                zhuanjiaBiandongPici.setBiandongrenshu((long)0);
                zhuanjiaBiandongPici.setZongrenshu((long)1);
                zhuanjiaBiandongPici.setIsvalid((long)1);

                Shiwu mmShiwu = new Shiwu();
                mmShiwu.setShiwuid(UUID.randomUUID().toString());
                mmShiwu.setDeptid(zhuanjiaBiandongPici.getDeptid());
                mmShiwu.setIsqidong((long)0);
                mmShiwu.setIsjieshu((long)0);
                mmShiwu.setChuangjianshijian(new Date());
                mmShiwu.setBeizhu1(mmId);
                mmShiwu.setIsvalid((long)1);

                return zhuanjiaBiandongPiciService.addZhuanjiabiandongpiciAndShiwu(zhuanjiaBiandongPici,mmShiwu)==true? JsonResult.getSuccessResult(zhuanjiaBiandongPici):JsonResult.getErrorResult("添加失败");
            } catch (Exception e) {
            logger.error("zhuanjiabiandongpici -> customadd: "+e.getMessage());
            return JsonResult.getErrorResult("添加专家变动批次失败");
        }
    }

    /**
     * 修改专家变动批次
     */
    @RequiresPermissions("system:zhuanjiabiandongpici:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZhuanjiaBiandongPici zhuanjiaBiandongPici = zhuanjiaBiandongPiciService.selectZhuanjiaBiandongPiciById(id);
        mmap.put("zhuanjiaBiandongPici", zhuanjiaBiandongPici);
        return prefix + "/edit";
    }

    /**
     * 修改保存专家变动批次
     */
    //@RequiresPermissions("system:zhuanjiabiandongpici:edit")
    @Log(title = "专家变动批次", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZhuanjiaBiandongPici zhuanjiaBiandongPici)
    {
        return toAjax(zhuanjiaBiandongPiciService.updateZhuanjiaBiandongPici(zhuanjiaBiandongPici));
    }

    /**
     * 删除专家变动批次
     */
    @RequiresPermissions("system:zhuanjiabiandongpici:remove")
    @Log(title = "专家变动批次", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zhuanjiaBiandongPiciService.deleteZhuanjiaBiandongPiciByIds(ids));
    }

    @PostMapping( "/find_custom_list")
    @ResponseBody
    public JsonResult<?> findCustomList(@RequestParam("deptid")String ppDeptId,@RequestParam("picimingcheng")String ppPicimingcheng){
        try{
            List<Map<String,Object>> mmList = zhuanjiaBiandongPiciService.findCustomList(ppDeptId,ppPicimingcheng);
            return JsonResult.getSuccessResult(mmList);
        } catch (Exception e) {
            logger.error("zhuanjiabiandongpici -> find_custom_list: "+e.getMessage());
            return JsonResult.getErrorResult("查询专家变动批次失败");
        }
    }

    @PostMapping( "/findone")
    @ResponseBody
    public JsonResult<?> findone(@RequestParam("id")String ppId){
        try{
            ZhuanjiaBiandongPici mmZhuanjiabiandongpici = zhuanjiaBiandongPiciService.selectZhuanjiaBiandongPiciById(ppId);
            return JsonResult.getSuccessResult(mmZhuanjiabiandongpici);
        } catch (Exception e) {
            logger.error("zhuanjiabiandongpici -> findone: "+e.getMessage());
            return JsonResult.getErrorResult("查询专家变动批次失败");
        }
    }
}
